package Hander.Add;



import View.Add.AddProjectAwardFormView;
import View.LeaderView;
import entity.ProjectAwardForm;
import service.PersonnelService;
import service.PersonnelServiceImpl;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddProjectAwardFormViewHandler implements ActionListener {
    private AddProjectAwardFormView addProjectAwardFormView;
    private LeaderView leaderView;

    public AddProjectAwardFormViewHandler(AddProjectAwardFormView addProjectAwardFormView, LeaderView leaderView) {
        this.addProjectAwardFormView = addProjectAwardFormView;
        this.leaderView = leaderView;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton = (JButton) e.getSource();
        String text = jButton.getText();
        if ("添加".equals(text)) {
            PersonnelService projectAwardFormService = new PersonnelServiceImpl();
            ProjectAwardForm projectAwardForm = addProjectAwardFormView.buildProjectAwardForm();
            Boolean addResult = projectAwardFormService.add(projectAwardForm.getAllAttributes(), addProjectAwardFormView.getTableName());
            if (addResult) {
                leaderView.reloadTable(addProjectAwardFormView.getTableName());
                JOptionPane.showMessageDialog(addProjectAwardFormView, "添加成功");
                addProjectAwardFormView.dispose();
            } else {
                System.out.println("添加错误！");
                JOptionPane.showMessageDialog(addProjectAwardFormView, "添加失败");
            }
        }
    }
}
